
import { computed, ref } from 'vue';
import { updateEntity, deleteEntity, updateExamTypeRelations } from '@/api/lms/exams';
import { Modal, Message } from '@arco-design/web-vue';
import { validateForm } from '@/utils';
import { getAvailableList, getExamTypeRelationList } from '@/api/types/types';

export let modalType = ref(0);
export let currRecord = ref({} as any);
export let examTypeRelations = ref([] as any);
export let simpleModeSwitch = ref(true)
export let compositeModeSwitch = ref(true)
export let hybirdModeSwitch = ref(true)

export function addNew () {
  modalType.value = 3;
  currRecord.value = {
    enabled: true
  };
  validateForm(null, true);
}

export function btnEdit (record:any) {
  currRecord.value = {...record};
  modalType.value = 2;
  validateForm(null, true);
}

export async function btnEditStructure (record:any) {
  currRecord.value = {...record};

  let relations = await getAvailableList();
  console.log('relations: ', relations);
  relations.forEach(e => {
      e.simpleMode = false;
      e.compositeMode = false;
      e.hybirdMode = false;
      e.typeDescription = e.problemTypeName + " | " + e.questionTypeName;
    });   

  console.log('relations: ', relations);

  let examRelations = await getExamTypeRelationList(record.examId);
  examRelations.forEach(e => {
    relations.filter(r => r.problemTypeId == e.problemTypeId && r.questionTypeId == e.questionTypeId)
      .forEach(m => {
        if (e.problemModeId == 145001) {
          m.simpleMode = true;
          //simpleModeSwitch.value = true;
        }
        if (e.problemModeId == 145002) {
          m.compositeMode = true;
          //compositeModeSwitch.value = true;
        }
        if (e.problemModeId == 145003) {
          m.hybirdMode = true;
          //hybirdModeSwitch.value = true;
        }
      })
  });  

  examTypeRelations.value = relations;

  modalType.value = 3;
  validateForm(null, true);
}

export function btnDelete (record:any, call:Function) {
  Modal.confirm({
    title: '确认',
    content: `确定要删除【${record.examName}】吗？`,
    onOk: async function() {
      await deleteEntity(record.examId);
      call();
    }
  });
}

export async function btnEditSave (call:Function) {
  if(!validateForm()) {
    return;
  }
  modalType.value = 0;
  await updateEntity(currRecord.value);
  call();
}

export async function btnEditStructSave (record:any,  call:Function) {
  if(!validateForm()) {
    return;
  }

  let command = {} as any;
  let commands = [] as any;
  command.examId = record.examId;
  examTypeRelations.value.forEach(e => {
    if (e.simpleMode) {
      let cmd = {} as any;
      cmd.examId = record.examId;
      cmd.problemModeId = 145001;
      cmd.problemTypeId = e.problemTypeId;
      cmd.questionTypeId = e.questionTypeId;
      commands.push(cmd); 
    }

    if (e.compositeMode) {
      let cmd = {} as any;
      cmd.examId = record.examId;
      cmd.problemModeId = 145002;
      cmd.problemTypeId = e.problemTypeId;
      cmd.questionTypeId = e.questionTypeId;
      commands.push(cmd); 
    }

    if (e.hybirdMode) {
      let cmd = {} as any;
      cmd.examId = record.examId;
      cmd.problemModeId = 145003;
      cmd.problemTypeId = e.problemTypeId;
      cmd.questionTypeId = e.questionTypeId;
      commands.push(cmd); 
    }
  })
  command.commands = commands;

  let success = await updateExamTypeRelations(command);
  if (success) {
    Message.success('修改试卷结构成功'); 
    modalType.value = 0;
    call();
  } else {
    Message.error('修改试卷结构失败'); 
  }
}
